<?php
declare (strict_types=1);

namespace app\common\model;

class Spec extends Base
{
    public function getSpecTextAttr($val, $data): array
    {
        return $data['spec'] ? json_decode($data['spec'], true) : [];
    }

    public static function _save($param, $options): array
    {
        try {
            foreach ($param['spec'] as $item) {
                if (empty($item['child'])) {
                    throw new \Exception('【' . $item['title'] . '】没有规格项，请添加');
                }
            }
            $param['spec'] = json_encode($param['spec']);
            if (!empty($param['id'])) {
                parent::update($param, ['id' => $param['id']]);
            } else {
                parent::create($param);
            }

            return ['code' => 1, 'msg' => '操作成功'];
        } catch (\Exception $e) {

            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }
}